package code;
import java.util.ArrayList;
public class printMatrix {
    public ArrayList<Integer> Solution(int [][] matrix) {
        ArrayList<Integer> re = new ArrayList<>();
        int row_s = 0,row_e = matrix.length-1,col_s=0,col_e=matrix[0].length-1;
        while(row_s<=row_e && col_s<=row_e){
            for(int i=col_s;i<=col_e;i++){
                re.add(matrix[col_s][i]);
            }
            if(re.size()>=matrix.length*matrix[0].length)break;
            row_s++;
            for(int i=row_s;i<=row_e;i++){
                re.add(matrix[i][col_e]);
            }
            if(re.size()>=matrix.length*matrix[0].length)break;
            col_e--;
            for(int i=col_e;i>=col_s;i--){
                re.add(matrix[row_e][i]);
            }
            if(re.size()>=matrix.length*matrix[0].length)break;
            row_e--;
            for(int i=row_e;i>=row_s;i--){
                re.add(matrix[i][col_s]);
            }
            col_s++;

        }
        return re;
    }
}
